<?php
/**
 * @title 79. 单词搜索
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $board
     * @param String $word
     * @return Boolean
     */
    function exist($board, $word) {
        /**
         * @since 2023-12-29 全局矩阵
         */
        $this->board = $board;

        /**
         * @since 2023-12-29 行数和列数
         */
        $rowCount = count($board);
        $colCount = count($board[0]);

        /**
         * @since 2023-12-29 单词的长度
         */
        $wordLength = strlen($word);

        /**
         * @since 2023-12-29 初始化一个矩阵，用于记录已经访问过的位置
         */
        $visited = array_fill(0, $rowCount, array_fill(0, $colCount, false));

        /**
         * @since 2023-12-29 从矩阵的第一个位置开始查找
         */
        for ($row=0; $row<$rowCount; $row++) {
            for ($col = 0; $col < $colCount; $col++) {
                /**
                 * @since 2023-12-29 元素值等于word[0]，则开始查找
                 */
                if($word[0] == $board[$row][$col]){
                    /**
                     * @since 2023-12-29 标记访问
                     */
                    $visited[$row][$col] = true;

                    /**
                     * @since 2023-12-29 第2个元素递归查找
                     */
                    if($this->find($word, 1, $wordLength, $visited, $row, $col)) {
                        return true;
                    } else {}

                    /**
                     * @since 2023-12-29 还原
                     */
                    $visited[$row][$col] = false;
                } else {}
            }
        }

        /**
         * @return Boolean
         */
        return false;
    }

    /**
     * @since 2023-12-29 递归查找
     * @param String $word
     * @param Int $i
     * @param Int $wordLength
     * @param Stirng[][] $visited
     * @param Int $row
     * @param Int $col
     * @return Boolean
     */
    function find($word, $i, $wordLength, $visited, $row, $col){
        /**
         * @since 2023-12-29 如果已经查找到最后一个元素，则返回true
         */
        if($i == $wordLength){
            return true;
        } else {}

        /**
         * @since 2023-12-29 从上下左右四个方向查找
         */
        $directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
        foreach ($directions as $direction) {
            $newRow = $row + $direction[0];
            $newCol = $col + $direction[1];

            /**
             * @since 2023-12-29 存在，未访问过，且值等于word[$i]，则开始查找
             */
            if(isset($this->board[$newRow][$newCol]) AND !$visited[$newRow][$newCol] AND $this->board[$newRow][$newCol] == $word[$i]){
                /**
                 * @since 2023-12-29 标记访问
                 */
                $visited[$newRow][$newCol] = true;

                /**
                 * @since 2023-12-29 下一个元素递归查找
                 */
                if($this->find($word, $i+1, $wordLength, $visited, $newRow, $newCol)) {
                    return true;
                } else {}

                /**
                 * @since 2023-12-29 还原
                 */
                $visited[$newRow][$newCol] = false;
            } else {}
        }

        /**
         * @return Boolean
         */
        return false;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.29_5.php
 */
$datas = [
    [[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED"], // true
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->exist(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
